package org.lyimg.com.imgapplication.utils;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.model.LazyHeaders;

import org.lyimg.com.imgapplication.R;

import uk.co.senab.photoview.PhotoView;

/**
 * Created by liuyong on 2017/5/3.
 */

public class PhotoDialog extends Dialog {

    String url;
    Context context;

    public PhotoDialog(Context context, String url) {
        super(context);
        this.url = url;
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.photo_dialo);
        getWindow().setBackgroundDrawable(new BitmapDrawable());
        getWindow().setLayout(AppUtils.getScreenWidth(context),AppUtils.getScreenHeight(context));

        PhotoView photo = findViewById(R.id.photo);
        ImageView img_down = findViewById(R.id.img_close);
        Glide.with(context).load(buildGlideUrl(url))
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .fitCenter()
                .crossFade()
                .into(photo);

        img_down.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                mhttpUtils.downImg("http://tnfs.tngou.net/img" + url, 1);
                dismiss();
            }
        });
    }

    private GlideUrl buildGlideUrl(String url) {
        if (TextUtils.isEmpty(url)) {
            return null;
        } else {
            return new GlideUrl(url, new LazyHeaders.Builder()
                    .addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
                    .addHeader("Accept-Encoding", "gzip, deflate, sdch, br")
                    .addHeader("Accept-Language", "zh-CN,zh;q=0.8")
                    .addHeader("Cache-Control", "max-age=0")
                    .addHeader("Connection", "keep-alive")
                    .addHeader("If-None-Match", "5c45772c-562c")
                    .addHeader("Upgrade-Insecure-Requests", "1")
                    .addHeader("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1")
                    .build());
        }
    }
}
